/** @file    FormModemDsl.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormModemDsl.cpp 2488 2010-05-21 01:48:27Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormModemDsl class.
 * The header for this class can be found in FormModemDsl.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormModemDsl.h"
 #include "ui_FormModemDsl.h"
 #include <interfaces/devices/ModemDsl.h>

 #include <core/Device.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormModemDsl, "wosh::gui::IWidgetGeneric", 1.0, _static_FormModemDsl )
 WOSH_GUI_MAP(wosh::gui::FormModemDsl, "wosh::interfaces::devices::ModemDsl", 0.0, _static_Map_FormModemDsl )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormModemDsl::FormModemDsl(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormModemDsl();
	this->m_ui->setupUi(this);

	this->setWindowTitle("Device ModemDsl");
	this->getWidgetURI().setName( Utilities::format("FormModemDsl#%10lu", Utilities::randomLong()) );

	connect(this->m_ui->wUpBarConfiguration, SIGNAL(updateClicked()), this, SLOT(refreshConfiguration()));
	connect(this->m_ui->wUpBarStatistics, SIGNAL(updateClicked()), this, SLOT(refreshStatistics()));

	this->propMapperCfg = new PropertiesMapper(this, true);
	connect(this->propMapperCfg, SIGNAL(send_request(const QString&, ISerializable*)), this, SLOT(executeRemoteMethod(const QString&, ISerializable*)));
	this->propMapperCfg->set(_ModemDsl_KEY_pppInterface, this->m_ui->editorHardwareInterface);
	this->propMapperCfg->set(_ModemDsl_KEY_RefreshFrequency, this->m_ui->editorRefresh);
	this->propMapperCfg->set(_ModemDsl_KEY_CtrlScript, this->m_ui->editorCtrlScript);
	this->propMapperCfg->set(_ModemDsl_KEY_PingCheckAddress, this->m_ui->editorPingAddress);

	this->propMapperStats = new PropertiesMapper(this, false);
	this->propMapperStats->set(_Device_KEY_DeviceStatus, this->m_ui->lblDeviceStatus);
	this->propMapperStats->set(_Modem_KEY_ConnectionStatus, this->m_ui->lblConnectionStatus);
	this->propMapperStats->set(_Modem_KEY_IpAddress, this->m_ui->lblIpAddress);
	this->propMapperStats->set(_Modem_KEY_ConnectedTime, this->m_ui->lblConnectedTime);
	this->propMapperStats->set(_Modem_KEY_statsSpeed, this->m_ui->lblConnectionSpeed);
	this->propMapperStats->set(_Modem_KEY_statsSpeedTop, this->m_ui->lblConnectionSpeedTop);
	this->propMapperStats->set(_Modem_KEY_statsPacketsTx, this->m_ui->lblTxPackets);
	this->propMapperStats->set(_Modem_KEY_statsPacketsRx, this->m_ui->lblRxPackets);
	this->propMapperStats->set(_Modem_KEY_statsBytesTx, this->m_ui->lblTxBytes);
	this->propMapperStats->set(_Modem_KEY_statsBytesRx, this->m_ui->lblRxBytes);
	this->propMapperStats->set(_Modem_KEY_statsErrorsTx, this->m_ui->lblTxErrors);
	this->propMapperStats->set(_Modem_KEY_statsErrorsRx, this->m_ui->lblRxErrors);

	mapMethodButton(_Modem_METHOD_connect, this->m_ui->btnConnect);
	mapMethodButton(_Modem_METHOD_disconnect, this->m_ui->btnDisconnect);
	mapMethodButton(_Modem_METHOD_reconnect, this->m_ui->btnReConnect);
	mapMethodButton(_Modem_METHOD_checkConnected, this->m_ui->bthCheckStatus);
	mapMethodButton(_Modem_METHOD_resetStats, this->m_ui->btnResetStats);

	connect(this->m_ui->btnRefreshAll, SIGNAL(clicked()), this, SLOT(refreshAll()));

	this->timerAutoRefresh = 0;
	chkAutoRefresh_stateChanged( Qt::Unchecked );
	connect(this->m_ui->chkAutoRefresh, SIGNAL(stateChanged(int)), this, SLOT(chkAutoRefresh_stateChanged(int)));
	connect(this->m_ui->spinAutoRefresh, SIGNAL(valueChanged(int)), this, SLOT(spinAutoRefresh_valueChanged(int)));

 }

FormModemDsl::~FormModemDsl() {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormModemDsl::refreshAll() {
	this->setWindowTitle( QString("%1 [%2]").arg(this->getTargetURI().getName().c_str()).arg(this->getTargetURI().getKernelName().c_str()) );
	executeRemoteMethod( _METHOD_ListProperties );
	this->m_ui->wUpBarConfiguration->sayRetrieving();
	this->m_ui->wUpBarStatistics->sayRetrieving();
 }

void FormModemDsl::refreshConfiguration() {
	List* dlProps = this->propMapperCfg->createKeysList();
	executeRemoteMethod( _METHOD_GetProperties, dlProps );
	this->m_ui->wUpBarConfiguration->sayRetrieving();
 }

void FormModemDsl::refreshStatistics() {
	List* dlProps = this->propMapperStats->createKeysList();
	executeRemoteMethod( _METHOD_GetProperties, dlProps );
	this->m_ui->wUpBarStatistics->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormModemDsl::chkAutoRefresh_stateChanged( int state ) {
	if ( state == Qt::Unchecked ) {
		this->m_ui->spinAutoRefresh->setEnabled(false);
		if ( this->timerAutoRefresh != 0 )
			killTimer(this->timerAutoRefresh);
	 }
	else {
		this->m_ui->spinAutoRefresh->setEnabled(true);
		if ( this->timerAutoRefresh != 0 )
			killTimer(this->timerAutoRefresh);
		if ( this->m_ui->spinAutoRefresh->value() != 0 )
			this->timerAutoRefresh = startTimer(this->m_ui->spinAutoRefresh->value()*1000);
	 }
 }

void FormModemDsl::spinAutoRefresh_valueChanged( int i ) {
	if ( this->timerAutoRefresh == 0 )
		this->timerAutoRefresh = startTimer(i*1000);
	else {
		killTimer(this->timerAutoRefresh);
		this->timerAutoRefresh = startTimer(i*1000);
	 }
 }

void FormModemDsl::timerEvent( QTimerEvent* ) {
	refreshStatistics();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormModemDsl::busMessage( const Message& message, const Bus* ) {
	if ( message.isEmpty() || !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) ) return;
	const wosh::Response* response = message.getContent()->asResponse();

	if ( this->propMapperCfg->evalResponse(response) )
		this->m_ui->wUpBarConfiguration->sayReady();

	if ( this->propMapperStats->evalResponse(response) )
		this->m_ui->wUpBarStatistics->sayReady();

	if ( WSUCCEEDED(response->getReturnValue()) && !this->m_ui->chkAutoRefresh->isChecked() ) {
		if ( response->getMethod() == _Modem_METHOD_connect	|| response->getMethod() == _Modem_METHOD_disconnect
			|| response->getMethod() == _Modem_METHOD_reconnect	|| response->getMethod() == _Modem_METHOD_checkConnected
			|| response->getMethod() == _Modem_METHOD_resetStats )
				{ refreshAll(); }
	 }
	promptMethodResponse(message);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
